package ar.com.nexttome;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.nexttome.context.ApplicationContext;
import ar.com.nexttome.entities.Mensaje;
import ar.com.nexttome.entities.Persona;
import ar.com.nexttome.util.Constantes;
import ar.com.nexttome.util.Tools;

public class PersonaActivity extends Activity {
	Persona personaSeleccionada = null;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.persona);

		Tools.createActionMenu(this, Constantes.STR_CONTACTO, MensajesActivity.class, R.drawable.ic_title_export_default);
		Tools.createActionMenu(this, Constantes.STR_CONTACTO, PerfilActivity.class, R.drawable.ic_title_home_default);

		personaSeleccionada = ((ApplicationContext) this.getApplicationContext()).getPersonaSeleccionada();

		TextView tvNick = (TextView) findViewById(R.id.textViewNick);
		TextView tvDistancia = (TextView) findViewById(R.id.textViewDistancia);
		TextView tvComentario = (TextView) findViewById(R.id.textViewComentario);
		ImageView image = (ImageView) findViewById(R.id.imageViewPersona);

		tvNick.setText(personaSeleccionada.getNick());
		tvDistancia.setText(String.valueOf(personaSeleccionada.getDistancia()));
		tvComentario.setText(String.valueOf(personaSeleccionada.getComentario()));

		Bitmap bitmap = Tools.base64ToBitmap(personaSeleccionada.getFoto());
		image.setImageBitmap(bitmap);
	}

	public void mandarMensaje(View view) {
		TextView tvMsg = (TextView) findViewById(R.id.etMsg);
		Persona yo = ((ApplicationContext) this.getApplicationContext()).getCurrentUser();

		Mensaje msg = new Mensaje();
		msg.setFrom(yo.getId());
		msg.setTo(personaSeleccionada.getId());
		msg.setMsg(tvMsg.getText().toString());

		if (!tvMsg.getText().toString().equals("")) {
			try {
				PersonaManager.mandarMensaje(msg);
				tvMsg.setText("");
				Toast.makeText(this, "mensaje enviado", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				Tools.alertReintentar(this.getApplicationContext(), Constantes.STR_MANDAR_MSG_ERROR);
				e.printStackTrace();
			}
		} else {
			Toast.makeText(this, Constantes.STR_PONER_MSG, Toast.LENGTH_LONG).show();
		}
	}

}
